//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.Chart.UI.Internals
{
    public class LinePanel : Panel
    {
        // Using a DependencyProperty as the backing store for ValueOrientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CategoryOrientationProperty =
            DependencyProperty.Register("CategoryOrientation", typeof (AxisOrientation), typeof (LinePanel),
                                        new FrameworkPropertyMetadata(AxisOrientation.MinimumToMaximum,
                                                                      FrameworkPropertyMetadataOptions.AffectsArrange));

        public static readonly DependencyProperty ValueOrientationProperty =
            DependencyProperty.Register("ValueOrientation", typeof (AxisOrientation), typeof (LinePanel),
                                        new FrameworkPropertyMetadata(AxisOrientation.MinimumToMaximum,
                                                                      FrameworkPropertyMetadataOptions.AffectsArrange));

        public AxisOrientation ValueOrientation
        {
            get { return (AxisOrientation) GetValue(ValueOrientationProperty); }
            set { SetValue(ValueOrientationProperty, value); }
        }


        public AxisOrientation CategoryOrientation
        {
            get { return (AxisOrientation) GetValue(CategoryOrientationProperty); }
            set { SetValue(CategoryOrientationProperty, value); }
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            var result = new Size();
            foreach (UIElement e in InternalChildren)
            {
                e.Measure(availableSize);
                result.Height = Math.Max(result.Height, e.DesiredSize.Height);
                result.Width = Math.Max(result.Width, e.DesiredSize.Width);
            }
            result = new Size(Math.Min(availableSize.Width, result.Width), Math.Min(availableSize.Height, result.Height));
            return result;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            ChartBase chart = ChartElement.GetChart(this);
            if (chart == null)
            {
                Debug.WriteLine("Problem with no chart");
                return finalSize;
            }
            AxisOrientation categoryOrientation = CategoryOrientation;
            AxisOrientation valueOrientation = ValueOrientation;
            Series series = SeriesElement.GetSeries(this);
            ChartLayout info = ChartToChartInfoConverter.GetInfo(chart);
            int maxIndex = SeriesElement.GetMaximumIndex(this);
            
            var yProjector = new ValueAxisProjector(AxisInfoHelper.GetMin(ChartElement.GetYAxisInfo(this)),
                                                    AxisInfoHelper.GetMax(ChartElement.GetYAxisInfo(this)), finalSize.Height, 0,
                                                    valueOrientation);
            foreach (FrameworkElement child in InternalChildren)
            {
                try
                {
                    double x = CategoryAxisPanel.GetScreenPosition(SeriesElement.GetIndex(child), 0, maxIndex, 0,
                                                                   finalSize.Width, categoryOrientation, true);
                        //\\for now

                    var nv = (NumericValue) child.DataContext;
                    double y = yProjector.GetValue(info.GetBounds(nv, series, 0).High);
                    var pt = new Point(x, y);

                    Size sz = child.DesiredSize;
                    pt.Offset(-sz.Width/2, - sz.Height/2);
                    child.Arrange(new Rect(pt, sz));
                }
                catch (Exception)
                {
                }
            }
            return finalSize;
        }

        // Using a DependencyProperty as the backing store for CategoryOrientation.  This enables animation, styling, binding, etc...
    }
}